package bottle.controller;

import bottle.entity.Wxbottle;
import bottle.entity.Wxuserbottle;
import bottle.service.impl.Wxbottleserviceimpl;
import config.Interceptor.PassToken;

import details.entity.Wxuserdetails;

import details.service.impl.Userdetailsserviceimpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import utils.CommonRetumType;
import utils.RedisUtil;
import wx.entity.Wxuser;
import wx.mapper.WxuserMapper;

import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/bottle")
public class Wxbottlecontroller{
    @Autowired
    Wxbottleserviceimpl wxbottleserviceimpl;

    @Autowired
    Userdetailsserviceimpl userdetailsserviceimpl;

    @Autowired
    WxuserMapper wxuserMapper;

    @Autowired
    RedisUtil redisUtil;



    @GetMapping("/create")
    @PassToken
    public CommonRetumType create(Wxbottle wxbottle)
    {
        Wxuserdetails wxuserdetails = userdetailsserviceimpl.selectOpenid(wxbottle.getOpenId());
        redisUtil.set(wxbottle.getOpenId(),wxuserdetails);

        if (wxbottle.getBottletype().equals("normal"))
        {
            if (wxuserdetails.getUserbasicsbottlecount() >0)
            {
                Integer reduce = wxuserdetails.getUserbasicsbottlecount()-1;
                Wxuserdetails wxuserdetails1 = new Wxuserdetails();
                wxuserdetails1.setOpenId(wxbottle.getOpenId());
                wxuserdetails1.setUserbasicsbottlecount(reduce);
                userdetailsserviceimpl.updateByPrimaryKeySelective(wxuserdetails1);
            }
            else {
                return CommonRetumType.create(0,"瓶子数量不足");
            }
        }else if (wxbottle.getBottletype().equals("love")){
            if (wxuserdetails.getUserpalbottlecount() >0)
            {
                Integer reducelove = wxuserdetails.getUserpalbottlecount()-1;
                Wxuserdetails wxuserdetails2 = new Wxuserdetails();
                wxuserdetails2.setOpenId(wxbottle.getOpenId());
                wxuserdetails2.setUserpalbottlecount(reducelove);
                userdetailsserviceimpl.updateByPrimaryKeySelective(wxuserdetails2);

            }else {
                return CommonRetumType.create(0,"交友瓶数量不足,请补充");
            }
        }
        wxbottle.setBottlecreatetime(new Date());
        wxbottle.setOpenId(wxbottle.getOpenId());
        return CommonRetumType.create(wxbottleserviceimpl.insertSelective(wxbottle),"漂流瓶已投掷");
    }


    @GetMapping("/insert")
    @PassToken
    public CommonRetumType insert(@RequestParam("openId") String openid,@RequestParam("bottleid") Integer bottleid)
    {
        Wxuserbottle wxuserbottle = new Wxuserbottle();
        wxuserbottle.setBottleid(bottleid);
        wxuserbottle.setOpenId(openid);

        return CommonRetumType.create(wxbottleserviceimpl.insertSelectiveuser(wxuserbottle),"关系添加成功");

    }

    @GetMapping("/read")
    @PassToken
    public CommonRetumType read(@RequestParam("openId") String openid)
    {
        return CommonRetumType.create(wxbottleserviceimpl.selectByPrimaryKey(openid),"漂流瓶获取成功");
    }

    @GetMapping("/index")
    @PassToken
    public CommonRetumType index(@RequestParam("openId") String openid)
    {
        //随机取出一条数据
        Wxbottle wxbottle = wxbottleserviceimpl.selectBYReadpt(openid);

        int reducecount = 0;
        int addcount = 0;

        if (wxbottle == null){
            return CommonRetumType.create(null,"数据库瓶子取完");
        }
        Wxuser wxuser = wxuserMapper.selectByPrimaryKey(wxbottle.getOpenId());

        if (wxuser == null){
            return CommonRetumType.create(wxbottle,"用户不存在");
        }

        Wxuserdetails wxuserdetails = userdetailsserviceimpl.selectOpenid(wxbottle.getOpenId());
        if (wxuserdetails == null){
            return CommonRetumType.create("null","无打捞用户的详细");
        }
        Wxuserdetails wxuserdetails2 = userdetailsserviceimpl.selectOpenid(openid);
        if (wxuserdetails2.getUserfishingcount()==0){
            return CommonRetumType.create(0,"渔网数量不足");
        }else {

            Wxuserdetails wxuserdetails1 = new Wxuserdetails();
            reducecount= wxuserdetails2.getUserfishingcount()-1;
            wxuserdetails1.setOpenId(openid);
            wxuserdetails1.setUserfishingcount(reducecount);


            addcount=wxbottle.getBottlecount()+1;

            Wxbottle wxbottle1 = new Wxbottle();
            wxbottle1.setBottlecount(addcount);
            wxbottle1.setBottleid(wxbottle.getBottleid());
            wxbottleserviceimpl.updateByPrimaryKeySelective(wxbottle1);
            userdetailsserviceimpl.updateByPrimaryKeySelective(wxuserdetails1);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("bottle",wxbottle);
        hashMap.put("userdetails",wxuserdetails);
        hashMap.put("userimage",wxuser.getAvatarUrl());
        return CommonRetumType.create(hashMap,"捞取成功");
    }


    @PutMapping("update")
    @PassToken
    public CommonRetumType update(Wxbottle wxbottle)
    {
        return CommonRetumType.create(wxbottle,"微信信息更新成功");
    }

    @DeleteMapping("/delete")
    @PassToken
    public CommonRetumType delete(Integer id)
    {
        return CommonRetumType.create(wxbottleserviceimpl.deleteByPrimaryKey(id),"漂流瓶删除成功");
    }
}
